package com.travelbid.common;

import com.travelbid.common.PropertyTool;

public class Paging
{
	 
	public static final String PAGE_SIZE_STR = PropertyTool.getValue("page.size.str");
	public static final String PAGE_CURRENT_STR = PropertyTool.getValue("page.current.str");
	public static final String PAGE_TOTAL_STR = PropertyTool.getValue("page.total.str");
	
	 private int pageSize = Integer.parseInt(PropertyTool.getValue("default.page.size"));//ҳ���С
	 private int currentPage = 1;//��ǰҳ��
	 private long totalCount = -1;
	 private String action;
	 private String connector;
	 private int pageList = Integer.parseInt(PropertyTool.getValue("page.list"));
	 

 Paging()
 {
     currentPage = 1;
     totalCount = -1;
     action = "";
     connector = "";
 }

 public int getPageSize()
 {
     return pageSize;
 }

 public void setPageSize(int pageSize)
 {
     this.pageSize = pageSize;
 }

 public int getCurrentPage()
 {
     return currentPage;
 }

 public void setCurrentPage(int currentPage)
 {
     this.currentPage = currentPage;
     recalculate();
 }

 public long getTotalCount()
 {
     return totalCount;
 }

 public void setTotalCount(long totalCount)
 {
     this.totalCount = totalCount;
     recalculate();
 }

 public int getPageCount()//��ҳ��
 {
     int count = (int)Math.ceil((double)totalCount / (double)pageSize);
     return count;
 }

 
 private void recalculate()
 {
     int pageCount = getPageCount();
     if(currentPage < 1)
         currentPage = 1;    
 }

 public String toString()
 {
	 int pageCount = getPageCount();
	 if(currentPage > pageCount)
         currentPage = pageCount;
	 
	
	 String rowStart = "<ul>";
	 String first = ""; 
	 String prev = "";
	 String pageNumTemp ="<li><A href=\""+action+connector+PAGE_SIZE_STR+"="+pageSize + "&" + PAGE_TOTAL_STR + "=" + totalCount +"&" +PAGE_CURRENT_STR+"=*\">*</A></li>&nbsp;&nbsp;";
	 String pageNum = "<li>"+currentPage+"</li>&nbsp;&nbsp;";
	 String pageResult = "";
	 String next = "";
	 String last = "";
	 String total = "<li>(��"+pageCount+"ҳ)</li>";
	 String rowEnd = "</ul>";
	 
	 if(pageCount > pageList){
		 //	�ж�first�Ƿ����, pageListӦ���Ǹ�����
		 if(currentPage - 1 > pageList / 2){
			first = "<li><A href=\""+action+connector+PAGE_SIZE_STR+"="+pageSize + "&" + PAGE_TOTAL_STR + "=" + totalCount + "&" +PAGE_CURRENT_STR+"="+1+"\">��һҳ</A></li>&nbsp;&nbsp;";
		 }
		 //�ж�last�Ƿ�Ӧ�ó���
		 if(pageCount - currentPage > pageList /2 ){
			 last = "<li><A href=\""+action+connector+PAGE_SIZE_STR+"="+pageSize + "&" + PAGE_TOTAL_STR + "=" + totalCount + "&" +PAGE_CURRENT_STR+"="+pageCount+"\">ĩҳ</A></li>&nbsp;&nbsp;";
		 } 
		 int i = 0; 
		 int j = 0;
		 while(true){
			 i++;
			 if(j >= pageList - 1)
				 break;
			 
			 if(currentPage - i >= 1){
				 pageNum = pageNumTemp.replaceAll("\\*", Integer.toString(currentPage - i)) + pageNum;
				 j++;
			 }
			 if(currentPage + i <= pageCount){
				 pageNum = pageNum + pageNumTemp.replaceAll("\\*", Integer.toString(currentPage + i));
				 j++;
			 }
		 }
		 pageResult = pageNum;
		 
		 
	 }
	 else{
		 for(int i = 1; i <= pageCount; i++){
			 if(i == currentPage){
				 pageResult += pageNum;
			 }
			 else{
				 pageResult += pageNumTemp.replaceAll("\\*", Integer.toString(i));
			 }
		 }
		 
	 }
	 if(currentPage != 1){
		 prev = "<li><A href=\""+action+connector+PAGE_SIZE_STR+"="+pageSize + "&" + PAGE_TOTAL_STR + "=" + totalCount + "&" +  PAGE_CURRENT_STR+"="+(currentPage-1)+"\">��һҳ</A></li>&nbsp;&nbsp;";
	 }
	 if(currentPage != pageCount){
		 next = "<li><A href=\""+action+connector+PAGE_SIZE_STR+"="+pageSize + "&" + PAGE_TOTAL_STR + "=" + totalCount + "&" + PAGE_CURRENT_STR+"="+(currentPage+1)+"\">��һҳ</A></li>&nbsp;&nbsp;";
	 }
     String ret = rowStart+first+prev+pageResult+next+last+total+rowEnd+"\n";
     //���ҳ��Ϊ0���Ͳ���ʾ��ҳ
     if(pageCount == 0){
    	 ret = "";
     }
     return ret;
 }

 public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
		if(action.indexOf("?") == -1){
			this.connector = "?";
		}
		else
			this.connector = "&";
	}

 
 	public static void main(String[] args) {
 		Paging page = new Paging();
 		page.setTotalCount(105);
 		page.setCurrentPage(1);
 		page.setAction("groupAction.do?id=118");
 		
 		System.out.println(page);
 	}
//20070621
 	 public String toStringWithClass()
 	 {
 		 int pageCount = getPageCount();
 		 if(currentPage > pageCount)
 	         currentPage = pageCount;
 		 
 		 String rowStart = "<ul>";
 		 String first = ""; 
 		 String prev = "";
 		 String pageNumTemp ="<li><a class=\"" + "pageText\"" + " href=\""+action+connector+PAGE_SIZE_STR+"="+pageSize + "&" + PAGE_TOTAL_STR + "=" + totalCount +"&" +PAGE_CURRENT_STR+"=*\">*</a></li>";
 		 String pageNum = "<li>"+currentPage+"</li>";
 		 String pageResult = "";
 		 String next = "";
 		 String last = "";
 		 String total = "";
 		 String rowEnd = "</ul>";
 		 
 		 if(pageCount > pageList){
 			 //	�ж�first�Ƿ����, pageListӦ���Ǹ�����
 			 if(currentPage - 1 > pageList / 2){
 				first = "<li><a class=\"" + "pageText\"" + " href=\""+action+connector+PAGE_SIZE_STR+"="+pageSize + "&" + PAGE_TOTAL_STR + "=" + totalCount + "&" +PAGE_CURRENT_STR+"="+1+"\">��һҳ</a></li>";
 			 }
 			 //�ж�last�Ƿ�Ӧ�ó���
 			 if(pageCount - currentPage > pageList /2 ){
 				 last = "<li><a class=\"" + "pageText\"" + " href=\""+action+connector+PAGE_SIZE_STR+"="+pageSize + "&" + PAGE_TOTAL_STR + "=" + totalCount + "&" +PAGE_CURRENT_STR+"="+pageCount+"\">ĩҳ</a></li>";
 			 } 
 			 int i = 0; 
 			 int j = 0;
 			 while(true){
 				 i++;
 				 if(j >= pageList - 1)
 					 break;
 				 
 				 if(currentPage - i >= 1){
 					 pageNum = pageNumTemp.replaceAll("\\*", Integer.toString(currentPage - i)) + pageNum;
 					 j++;
 				 }
 				 if(currentPage + i <= pageCount){
 					 pageNum = pageNum + pageNumTemp.replaceAll("\\*", Integer.toString(currentPage + i));
 					 j++;
 				 }
 			 }
 			 pageResult = pageNum;
 			 
 			 
 		 }
 		 else{
 			 for(int i = 1; i <= pageCount; i++){
 				 if(i == currentPage){
 					 pageResult += pageNum;
 				 }
 				 else{
 					 pageResult += pageNumTemp.replaceAll("\\*", Integer.toString(i));
 				 }
 			 }
 			 
 		 }
 		 if(currentPage != 1){
 			 prev = "<li><a class=\"" + "pageText\"" + " href=\""+action+connector+PAGE_SIZE_STR+"="+pageSize + "&" + PAGE_TOTAL_STR + "=" + totalCount + "&" +  PAGE_CURRENT_STR+"="+(currentPage-1)+"\">&lt;</a></li>";
 		 }
 		 if(currentPage != pageCount){
 			 next = "<li><a class=\"" + "pageText\"" + " href=\""+action+connector+PAGE_SIZE_STR+"="+pageSize + "&" + PAGE_TOTAL_STR + "=" + totalCount + "&" + PAGE_CURRENT_STR+"="+(currentPage+1)+"\">&gt;</a></li>";
 		 }
 	     String ret = rowStart+first+prev+pageResult+next+last+total+rowEnd+"\n";
 	     //���ҳ��Ϊ0���Ͳ���ʾ��ҳ
 	     if(pageCount == 0){
 	    	 ret = "";
 	     }
 	     return ret;
 	 }
	

}
